<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">填写问卷</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>填写问卷</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：<input id="paper-write-edit-search" class="layui-input search-input" type="text" placeholder="输入标题关键字"/>&emsp;
            <button id="paper-write-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="paper-write-table" lay-filter="paper-write-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="paper-write-state">
    <a class="layui-btn layui-btn layui-btn-xs" lay-event="write">填写问卷</a>
</script>
<script>
    layui.use(['form', 'table', 'config', 'admin'], function () {
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let admin = layui.admin;

        let loginUser = config.getUser();

        let url = config.base_server + 'api-paper/paper/getPapers';
        let params = {
            access_token: config.getToken().access_token,
            userid: loginUser.id
        };
        let cols = [[
            {type: 'numbers'},
            {field: 'ptitle', sort: true, title: '问卷标题'},
            {field: 'instru', sort: true, title: '问卷说明'},
            {field: 'publisStr', sort: true, title: '发布人'},
            {field: 'facegpStr', sort: true, title: '面对群体'},
            {field: 'respdtStr', sort: true, title: '调查对象'},
            {field: 'rtcodeStr', sort: true, title: '评分类型'},
            {field: 'pasort', sort: true, title: '排序号', width: 80},
            {field: 'endtim', sort: true, title: '结束日期'},
            {field: 'cretim', sort: true, title: '创建时间'},
            {
                field: 'stateStr', sort: true, title: '完成状态', width: 100, align: 'center', templet: e => {
                    return finishState(e.stateStr);
                }
            },
            {templet: '#paper-write-state', width: 120, align: 'center', title: '操作'}
        ]];

        // 渲染表格
        table.render({
            elem: '#paper-write-table',
            url: url,
            where: params,
            page: true,
            cols: cols
        });

        // 工具条点击事件
        table.on('tool(paper-write-table)', function (obj) {
            let layEvent = obj.event;
            if (layEvent === 'write') {
                layer.confirm('确定填写问卷吗？', function (index) {
                    layer.load(2);
                    admin.req('api-paper/paper/findPaperRecord?papeid=' + obj.data.papeid + '&userid=' + loginUser.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code === 0) {
                            layer.close(index);
                            writePaper(obj.data);
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'GET');
                });
            }
        });

        // 搜索按钮点击事件
        $('#paper-write-btn-search').click(function () {
            let key = $('#paper-write-edit-search').val();
            table.reload('paper-write-table', {where: {searchKey: key}});
        });

        // 填写问卷
        let writePaper = function (data) {
            admin.putTempData('t_writePaper', data);
            let title = '填写问卷';
            admin.popupCenter({
                title: title,
                path: 'pages/paper/paperWrite_form.html',
                area: '1200px',
                offset: '50px',
                finish: function () {
                    table.reload('paper-write-table', {});
                }
            });
        };

        /**
         * 完成状态
         * @param stateStr
         */
        function finishState(stateStr) {
            if (stateStr === '已完成') {
                return '<a class="layui-btn layui-btn-xs layui-btn-warm">已完成</a>';
            } else
                return '<a class="layui-btn layui-btn-xs layui-btn-normal">未完成</a>';
        }
    });
</script>